/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：ReqCfgDaoImpl.java
 * History:
 *         2012-6-7: Initially created, Chrise.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.BaseDao;
import com.xteam.ecsystem.dao.IReqCfgDao;
import com.xteam.ecsystem.entities.SysRequestConfig;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 请求配置数据访问实现。
 * @author Chrise
 */
@Repository
public class ReqCfgDaoImpl implements IReqCfgDao {
	
	@Autowired
	private BaseDao<SysRequestConfig> baseDao;
	
	/**
	 * @see com.xteam.ecsystem.dao.IReqCfgDao#getConfigsByNameAndState(java.lang.String, int, int, int)
	 */
	public PageCenterStation<SysRequestConfig> getConfigsByNameAndState(String name, int state, int first, int max) {
		String cdt = "";
		if (name != INVALID_NAME) cdt = " where srcName like '%" + name + "%'";
		if (state != INVALID_STATE) {
			if ("".equals(cdt)) {
				cdt = " where srcState = " + state;
			} else {
				cdt += " and srcState = " + state;
			}
		}
		
		String countHql = "select count(srcId) from SysRequestConfig" + cdt;
		String queryHql = "from SysRequestConfig" + cdt + " order by srcRelative";
		
		return this.baseDao.queryPage(countHql, queryHql, first, max);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IReqCfgDao#updateState(java.lang.String, int)
	 */
	public void updateState(String id, int state) {
		String hql = "update SysRequestConfig set srcState = " + state + " where srcId = '" + id + "'";
		this.baseDao.executeUpdate(hql);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IReqCfgDao#deleteConfigs(java.util.List)
	 */
	public void deleteConfigs(List<String> ids) {
		String cdt = "'" + ids.get(0) + "'";
		for (int index = 1; index < ids.size(); index ++) {
			cdt += ", '" + ids.get(index) + "'";
		}
		
		String hql = "delete from SysRequestConfig where srcId in (" + cdt + ")";
		this.baseDao.executeUpdate(hql);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IReqCfgDao#isExist(java.lang.String)
	 */
	public boolean isExist(String request) {
		String hql = "select count(srcId) from SysRequestConfig where srcRequest = '" + request + "'";
		if (this.baseDao.count(hql) > 0) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IReqCfgDao#isExist(java.lang.String, java.lang.String)
	 */
	public boolean isExist(String id, String request) {
		String hql = "select count(srcId) from SysRequestConfig where srcId <> '" + id + 
			"' and srcRequest = '" + request + "'";
		if (this.baseDao.count(hql) > 0) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IReqCfgDao#insertConfig(com.xteam.ecsystem.entities.SysRequestConfig)
	 */
	public void insertConfig(SysRequestConfig src) {
		this.baseDao.save(src);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IReqCfgDao#updateConfig(com.xteam.ecsystem.entities.SysRequestConfig)
	 */
	public void updateConfig(SysRequestConfig src) {
		this.baseDao.update(src);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IReqCfgDao#getConfig(java.lang.String)
	 */
	public SysRequestConfig getConfig(String id) {
		String hql = "from SysRequestConfig where srcId = '" + id + "'";
		return this.baseDao.findUniqueObject(hql);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IReqCfgDao#getConfigsByState(int)
	 */
	public List<SysRequestConfig> getConfigsByState(int state) {
		String hql = "from SysRequestConfig where srcState = " + state;
		return this.baseDao.queryList(hql);
	}
}
